package com.example.demo.command;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

public class Command {
    public static void main(String[] args) throws IOException, InterruptedException {
        Process exec = Runtime.getRuntime().exec("ping baidu.com");
        exec.waitFor(3, TimeUnit.SECONDS);
        StringBuffer sb = new StringBuffer();
        try(InputStream inputStream = exec.getInputStream()) {
            byte[] bytes = new byte[1024];
            int read = -1;
            while ((read = inputStream.read(bytes)) != -1) {
                sb.append(new String(bytes, 0, read, System.getProperty("sun.jnu.encoding")));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        int exitValue = exec.exitValue();
        System.out.println("退出码："+exitValue);
        System.out.println("输出："+sb.toString());
        System.out.println("网络连通性："+(exitValue == 0 ? "连通" : "不连通"));
    }
}
